package com.sunshine.lnuplus.utils

import com.sunshine.lnuplus.callback.LoginCallBack
import com.sunshine.lnuplus.model.ScoreBean
import com.sunshine.lnuplus.model.ScoreData
import org.jsoup.select.Elements

/**
 * Created by sunshine on 2020/3/13
 */

object LoadScoreUtils{

    private lateinit var result: Elements
    private lateinit var loginCallBack: LoginCallBack
    private lateinit var user: String

    fun init(
        result: Elements,
        loginCallBack: LoginCallBack,
        user: String
    ){
        this.result = result
        this.loginCallBack = loginCallBack
        this.user = user
        resolve()
    }

    private fun resolve() {
        val scoreInfo = arrayOfNulls<String>(4) //0课程名称，1学分，2成绩，3课程类型
        val scoreList = ArrayList<ScoreBean>()
        for ((count, element) in result.withIndex()) {
            var info: String = element.toString()
            info = info
                .replace("<td bgcolor=\"#EAE2F3\"><p align=\"center\">", "")
                .replace("</p></td>", "")
            when (count % 8) {
                2 -> scoreInfo[0] = info
                4 -> scoreInfo[1] = info
                6 -> scoreInfo[2] = info
                7 -> {
                    scoreInfo[3] = info
                    val score = ScoreBean(
                        scoreInfo[0]!!,
                        scoreInfo[1]!!,
                        if(scoreInfo[2]!!.isEmpty()) "-" else scoreInfo[2]!!,
                        scoreInfo[3]!!
                    )
                    scoreList.add(score)
                }
            }
        }
        ScoreData.scoreList = scoreList
        loginCallBack.onSuccess()
    }
}